/*     */ package com.xhgk.dao.impl;
/*     */ 
/*     */ import com.xhgk.dao.PeriodicalDao;
/*     */ import com.xhgk.domain.ConstantParam;
/*     */ import com.xhgk.domain.Info;
/*     */ import com.xhgk.domain.Paging;
/*     */ import com.xhgk.domain.Periodical;
/*     */ import com.xhgk.utis.LuceneTool;
/*     */ import com.xhgk.utis.StringUtils;
/*     */ import java.io.IOException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Comparator;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.TreeMap;
/*     */ import java.util.TreeSet;
/*     */ import org.apache.commons.lang.StringUtils;
/*     */ import org.apache.lucene.document.Document;
/*     */ import org.apache.lucene.document.Fieldable;
/*     */ import org.apache.lucene.index.Term;
/*     */ import org.apache.lucene.queryParser.QueryParser;
/*     */ import org.apache.lucene.search.BooleanClause;
/*     */ import org.apache.lucene.search.BooleanQuery;
/*     */ import org.apache.lucene.search.Collector;
/*     */ import org.apache.lucene.search.IndexSearcher;
/*     */ import org.apache.lucene.search.Query;
/*     */ import org.apache.lucene.search.ScoreDoc;
/*     */ import org.apache.lucene.search.Sort;
/*     */ import org.apache.lucene.search.SortField;
/*     */ import org.apache.lucene.search.TermQuery;
/*     */ import org.apache.lucene.search.TermRangeQuery;
/*     */ import org.apache.lucene.search.TopDocs;
/*     */ import org.apache.lucene.search.TopFieldCollector;
/*     */ import org.apache.lucene.search.TopFieldDocs;
/*     */ import org.apache.lucene.util.NumericUtils;
/*     */ import org.apache.lucene.util.Version;
/*     */ 
/*     */ public class PeriodicalDaoImpl
/*     */   implements PeriodicalDao {
/*     */   public TreeSet<Periodical> getPeriodicals(Periodical periodical) throws IOException {
/*  43 */     IndexSearcher searcher = LuceneTool.getSearcher();
/*  44 */     BooleanQuery query = new BooleanQuery();
/*  45 */     query.add((Query)new TermQuery(new Term("library", periodical.getLibrary())), BooleanClause.Occur.MUST);
/*  46 */     query.add((Query)new TermQuery(new Term("year", periodical.getYear().toString())), BooleanClause.Occur.MUST);
/*  47 */     TopDocs search = searcher.search((Query)query, searcher.maxDoc());
/*  48 */     TreeSet<Periodical> periodicals = new TreeSet<Periodical>(new Comparator<Periodical>()
/*     */         {
/*     */           public int compare(Periodical p1, Periodical p2) {
/*  51 */             return (p1.getIssue().intValue() == p2.getIssue().intValue()) ? 0 : ((p2.getIssue().intValue() > p1.getIssue().intValue()) ? -1 : 1);
/*     */           }
/*     */         });
/*  54 */     Periodical info = new Periodical(); byte b; int i; ScoreDoc[] arrayOfScoreDoc;
/*  55 */     for (i = (arrayOfScoreDoc = search.scoreDocs).length, b = 0; b < i; ) { ScoreDoc scoreDoc = arrayOfScoreDoc[b];
/*     */       
/*  57 */       Document doc = searcher.doc(scoreDoc.doc);
/*  58 */       info.setIssue(Integer.valueOf(NumericUtils.prefixCodedToInt(doc.get("issue"))));
/*  59 */       if (!periodicals.contains(info)) {
/*     */         
/*  61 */         info.setId(Integer.valueOf(scoreDoc.doc));
/*  62 */         info.setDate(doc.get("date"));
/*  63 */         info.setImg(String.format("%s/%s.jpg", new Object[] { periodical.getLibrary(), info.getDate() }));
/*  64 */         info.setPdf(String.format("%s/%s.pdf", new Object[] { periodical.getLibrary(), info.getDate() }));
/*  65 */         periodicals.add(info);
/*  66 */         info = new Periodical();
/*     */       }  b++; }
/*     */     
/*  69 */     return periodicals;
/*     */   }
/*     */ 
/*     */   
/*     */   public Periodical getCatalogue(Integer id) throws IOException {
/*  74 */     Periodical periodical = new Periodical();
/*  75 */     IndexSearcher searcher = LuceneTool.getSearcher();
/*  76 */     Document doc = searcher.doc(id.intValue());
/*  77 */     periodical.setLibrary(doc.get("library"));
/*  78 */     periodical.setDate(doc.get("date"));
/*  79 */     periodical.setYear(Integer.valueOf(Integer.parseInt(doc.get("year"))));
/*  80 */     int issue = NumericUtils.prefixCodedToInt(doc.get("issue"));
/*  81 */     periodical.setIssue(Integer.valueOf(issue));
/*  82 */     periodical.setImg(String.format("%s/%s.jpg", new Object[] { periodical.getLibrary(), periodical.getDate() }));
/*     */ 
/*     */     
/*  85 */     BooleanQuery query = new BooleanQuery();
/*  86 */     query.add((Query)new TermQuery(new Term("library", periodical.getLibrary())), BooleanClause.Occur.MUST);
/*  87 */     query.add((Query)new TermQuery(new Term("year", periodical.getYear().toString())), BooleanClause.Occur.MUST);
/*  88 */     query.add((Query)new TermQuery(new Term("issue", NumericUtils.intToPrefixCoded(issue))), BooleanClause.Occur.MUST);
/*     */     
/*  90 */     Sort sort = new Sort(new SortField("index", 4, false));
/*  91 */     TopFieldDocs topFieldDocs = searcher.search((Query)query, searcher.maxDoc(), sort);
/*  92 */     List<Info> infos = new ArrayList<Info>(((TopDocs)topFieldDocs).totalHits); byte b; int j; ScoreDoc[] arrayOfScoreDoc1;
/*  93 */     for (j = (arrayOfScoreDoc1 = ((TopDocs)topFieldDocs).scoreDocs).length, b = 0; b < j; ) { ScoreDoc scoreDoc = arrayOfScoreDoc1[b];
/*     */       
/*  95 */       Info info = new Info();
/*  96 */       doc = searcher.doc(scoreDoc.doc);
/*  97 */       info.setId(Integer.valueOf(scoreDoc.doc));
/*  98 */       String ellipsis = StringUtils.setEllipsis(doc.get("title"), 30);
/*  99 */       ellipsis = ellipsis.replaceAll("^　+", "").replaceAll("‘", "“").replaceAll("’", "”");
/* 100 */       info.setTitle(ellipsis);
/* 101 */       info.setAuthor(StringUtils.setEllipsis(doc.get("author"), 6));
/* 102 */       infos.add(info); b++; }
/*     */     
/* 104 */     periodical.setInfos(infos);
/*     */ 
/*     */     
/* 107 */     query = new BooleanQuery();
/* 108 */     query.add((Query)new TermQuery(new Term("library", periodical.getLibrary())), BooleanClause.Occur.MUST);
/* 109 */     query.add((Query)new TermQuery(new Term("year", periodical.getYear().toString())), BooleanClause.Occur.MUST);
/* 110 */     ScoreDoc[] scoreDocs = (searcher.search((Query)query, 2000, sort)).scoreDocs;
/* 111 */     TreeMap<Integer, Integer> temps = new TreeMap<Integer, Integer>(new Comparator<Integer>() {
/*     */           public int compare(Integer t1, Integer t2) {
/* 113 */             return (t1.intValue() == t2.intValue()) ? 0 : ((t2.intValue() > t1.intValue()) ? -1 : 1);
/*     */           }
/*     */         });
/* 116 */     for (int i = 0; i < scoreDocs.length; i++)
/*     */     {
/* 118 */       temps.put(Integer.valueOf(NumericUtils.prefixCodedToInt(searcher.doc((scoreDocs[i]).doc).get("issue"))), Integer.valueOf((scoreDocs[i]).doc));
/*     */     }
/* 120 */     Object[] array = temps.keySet().toArray();
/* 121 */     for (int k = 0; k < array.length; k++) {
/*     */       
/* 123 */       if (Integer.parseInt(String.valueOf(array[k])) == issue) {
/*     */         
/* 125 */         if (k + 1 < array.length)
/*     */         {
/* 127 */           periodical.setNext_peri_id(temps.get(array[k + 1]));
/*     */         }
/* 129 */         if (k - 1 >= 0)
/*     */         {
/* 131 */           periodical.setPrior_peri_id(temps.get(array[k - 1]));
/*     */         }
/*     */         break;
/*     */       } 
/*     */     } 
/* 136 */     temps.clear();
/* 137 */     temps = null;
/* 138 */     return periodical;
/*     */   }
/*     */ 
/*     */   
/*     */   public Periodical getInfo(Integer id) throws IOException {
/* 143 */     IndexSearcher searcher = LuceneTool.getSearcher();
/*     */     
/* 145 */     Periodical periodical = new Periodical();
/* 146 */     Document doc = searcher.doc(id.intValue());
/* 147 */     periodical.setLibrary(doc.get("library"));
/* 148 */     periodical.setYear(Integer.valueOf(Integer.parseInt(doc.get("year"))));
/* 149 */     periodical.setIssue(Integer.valueOf(NumericUtils.prefixCodedToInt(doc.get("issue"))));
/*     */     
/* 151 */     Info info = new Info();
/* 152 */     info.setId(id);
/* 153 */     info.setTitle(doc.get("title").replaceAll("‘", "“").replaceAll("’", "”"));
/* 154 */     String text = doc.get("text").replaceAll("<%basePath%>", "/researchFile/".concat(periodical.getLibrary()));
/* 155 */     text = text.replaceAll("‘", "“").replaceAll("’", "”");
/* 156 */     info.setContent(text);
/* 157 */     info.setAuthor(doc.get("author"));
/* 158 */     String index = doc.get("index");
/*     */     
/* 160 */     info.setAttrs(new TreeMap<Object, Object>(new Comparator<String>()
/*     */           {
/*     */             public int compare(String s1, String s2) {
/* 163 */               return 1;
/*     */             }
/*     */           }));
/* 166 */     String name = null;
/* 167 */     for (Fieldable fieldable : doc.getFields()) {
/*     */       
/* 169 */       name = fieldable.name();
/* 170 */       if (!name.matches("^[\\w$]+"))
/*     */       {
/* 172 */         info.getAttrs().put(name, fieldable.stringValue());
/*     */       }
/*     */     } 
/* 175 */     periodical.setInfo(info);
/*     */     
/* 177 */     BooleanQuery query = new BooleanQuery();
/* 178 */     query.add((Query)new TermQuery(new Term("library", periodical.getLibrary())), BooleanClause.Occur.MUST);
/* 179 */     query.add((Query)new TermQuery(new Term("year", periodical.getYear().toString())), BooleanClause.Occur.MUST);
/* 180 */     query.add((Query)new TermQuery(new Term("issue", NumericUtils.intToPrefixCoded(periodical.getIssue().intValue()))), BooleanClause.Occur.MUST);
/* 181 */     TopFieldDocs topFieldDocs = searcher.search((Query)query, searcher.maxDoc(), new Sort(new SortField("index", 4, false)));
/* 182 */     List<Info> infos = new ArrayList<Info>(((TopDocs)topFieldDocs).totalHits);
/*     */ 
/*     */     
/* 185 */     int next_article_id = -1;
/* 186 */     int prior_article_id = -1;
/* 187 */     if (StringUtils.isNotEmpty(index)) {
/* 188 */       int indexNumber = NumericUtils.prefixCodedToInt(index); byte b; int i; ScoreDoc[] arrayOfScoreDoc;
/* 189 */       for (i = (arrayOfScoreDoc = ((TopDocs)topFieldDocs).scoreDocs).length, b = 0; b < i; ) { ScoreDoc scoreDoc = arrayOfScoreDoc[b];
/*     */         
/* 191 */         info = new Info();
/* 192 */         doc = searcher.doc(scoreDoc.doc);
/* 193 */         info.setId(Integer.valueOf(scoreDoc.doc));
/*     */         
/* 195 */         info.setTitle(doc.get("title").replaceAll("^　+", "").replaceAll("‘", "“").replaceAll("’", "”"));
/* 196 */         infos.add(info);
/*     */ 
/*     */         
/* 199 */         int currIndex = NumericUtils.prefixCodedToInt(doc.get("index"));
/* 200 */         if (next_article_id == -1 && indexNumber + 1 == currIndex) {
/*     */           
/* 202 */           next_article_id = scoreDoc.doc;
/* 203 */           periodical.setNext_article_id(Integer.valueOf(next_article_id));
/* 204 */           periodical.setNext_article_title(StringUtils.setEllipsis(info.getTitle(), 20));
/*     */         }
/* 206 */         else if (prior_article_id == -1 && indexNumber - 1 == currIndex) {
/*     */           
/* 208 */           prior_article_id = scoreDoc.doc;
/* 209 */           periodical.setPrior_article_id(Integer.valueOf(prior_article_id));
/* 210 */           periodical.setPrior_article_title(StringUtils.setEllipsis(info.getTitle(), 20));
/*     */         }  b++; }
/*     */     
/*     */     } else {
/*     */       byte b; int i; ScoreDoc[] arrayOfScoreDoc;
/* 215 */       for (i = (arrayOfScoreDoc = ((TopDocs)topFieldDocs).scoreDocs).length, b = 0; b < i; ) { ScoreDoc scoreDoc = arrayOfScoreDoc[b];
/*     */         
/* 217 */         info = new Info();
/* 218 */         doc = searcher.doc(scoreDoc.doc);
/* 219 */         info.setId(Integer.valueOf(scoreDoc.doc));
/* 220 */         info.setTitle(doc.get("title"));
/* 221 */         infos.add(info);
/*     */         
/* 223 */         if (scoreDoc.doc > id.intValue()) {
/* 224 */           if (next_article_id == -1) {
/* 225 */             next_article_id = scoreDoc.doc;
/*     */           } else {
/* 227 */             next_article_id = Math.min(next_article_id, scoreDoc.doc);
/*     */           } 
/* 229 */         } else if (scoreDoc.doc < id.intValue()) {
/* 230 */           if (prior_article_id == -1) {
/* 231 */             prior_article_id = scoreDoc.doc;
/*     */           } else {
/* 233 */             prior_article_id = Math.max(prior_article_id, scoreDoc.doc);
/*     */           } 
/*     */         }  b++; }
/* 236 */        if (next_article_id != -1) {
/* 237 */         doc = searcher.doc(next_article_id);
/* 238 */         periodical.setNext_article_id(Integer.valueOf(next_article_id));
/* 239 */         periodical.setNext_article_title(StringUtils.setEllipsis(doc.get("title"), 20));
/*     */       } 
/* 241 */       if (prior_article_id != -1) {
/* 242 */         doc = searcher.doc(prior_article_id);
/* 243 */         periodical.setPrior_article_id(Integer.valueOf(prior_article_id));
/* 244 */         periodical.setPrior_article_title(StringUtils.setEllipsis(doc.get("title"), 20));
/*     */       } 
/*     */     } 
/* 247 */     periodical.setInfos(infos);
/* 248 */     infos = null;
/* 249 */     return periodical;
/*     */   }
/*     */ 
/*     */   
/*     */   public void search(Periodical periodical) {
/* 254 */     BooleanQuery bqs = new BooleanQuery();
/* 255 */     if ("all".equalsIgnoreCase(periodical.getSearch_field())) {
/*     */       
/* 257 */       List<String> participle = LuceneTool.getParticiple(periodical.getKeyword()); byte b; int i; String[] arrayOfString;
/* 258 */       for (i = (arrayOfString = ConstantParam.LUCENE_FIELE).length, b = 0; b < i; ) { String field = arrayOfString[b];
/*     */         
/* 260 */         BooleanQuery bq = new BooleanQuery();
/* 261 */         for (String str : participle)
/*     */         {
/* 263 */           bq.add((Query)new TermQuery(new Term(field, str)), BooleanClause.Occur.MUST);
/*     */         }
/* 265 */         bqs.add((Query)bq, BooleanClause.Occur.SHOULD); b++; }
/*     */       
/* 267 */       for (i = (arrayOfString = ConstantParam.LUCENE_DEFAULT_FIELE).length, b = 0; b < i; ) { String field = arrayOfString[b];
/*     */         
/* 269 */         BooleanQuery bq = new BooleanQuery();
/* 270 */         for (String str : participle)
/*     */         {
/* 272 */           bq.add((Query)new TermQuery(new Term(field, str)), BooleanClause.Occur.MUST);
/*     */         }
/* 274 */         bqs.add((Query)bq, BooleanClause.Occur.SHOULD); b++; }
/*     */     
/*     */     } else {
/* 277 */       List<String> participle1 = LuceneTool.getParticiple(periodical.getKeyword());
/* 278 */       for (String str : participle1)
/*     */       {
/* 280 */         bqs.add((Query)new TermQuery(new Term(periodical.getSearch_field(), str)), BooleanClause.Occur.MUST);
/*     */       }
/*     */     } 
/* 283 */     periodical.setQuery(bqs.toString());
/*     */   }
/*     */ 
/*     */   
/*     */   public void pagper(Paging<Info> paging, Periodical periodical) throws Exception {
/* 288 */     IndexSearcher searcher = LuceneTool.getSearcher();
/*     */     
/* 290 */     BooleanQuery bqs = new BooleanQuery();
/* 291 */     QueryParser parser = new QueryParser(Version.LUCENE_36, "*", LuceneTool.getAnalyzer());
/* 292 */     bqs.add(parser.parse(periodical.getQuery()), BooleanClause.Occur.MUST);
/* 293 */     if (StringUtils.isNotEmpty(periodical.getSearch_library()))
/*     */     {
/* 295 */       bqs.add((Query)new TermQuery(new Term("library", periodical.getSearch_library())), BooleanClause.Occur.MUST);
/*     */     }
/* 297 */     TopDocs search = searcher.search((Query)bqs, searcher.maxDoc());
/* 298 */     paging.setTotalRow(search.totalHits);
/* 299 */     if (search.totalHits == 0) {
/*     */       return;
/*     */     }
/* 302 */     boolean order = periodical.getOrder();
/* 303 */     Sort sort = new Sort(new SortField("odate", 6, order));
/*     */     
/* 305 */     TopFieldCollector collector = TopFieldCollector.create(sort, paging.getRowNo() + paging.getEntry(), false, false, false, true);
/* 306 */     searcher.search((Query)bqs, (Collector)collector);
/* 307 */     TopDocs topDocs = collector.topDocs(paging.getRowNo(), paging.getEntry());
/* 308 */     List<Info> currData = new ArrayList<Info>(paging.getPageSize());
/* 309 */     Document doc = null;
/* 310 */     Info info = null; byte b; int i; ScoreDoc[] arrayOfScoreDoc;
/* 311 */     for (i = (arrayOfScoreDoc = topDocs.scoreDocs).length, b = 0; b < i; ) { ScoreDoc score = arrayOfScoreDoc[b];
/*     */       
/* 313 */       info = new Info();
/* 314 */       info.setId(Integer.valueOf(score.doc));
/* 315 */       doc = searcher.doc(score.doc);
/* 316 */       info.setDate(doc.get("date"));
/* 317 */       info.setAuthor(StringUtils.setHeightContent(doc.get("author"), periodical.getKeyword(), 6));
/* 318 */       info.setLibrary(doc.get("library"));
/* 319 */       info.setTitle(StringUtils.setHeightContent(doc.get("title"), periodical.getKeyword(), 32));
/* 320 */       info.setContent(StringUtils.setHeightContent(doc.get("content"), periodical.getKeyword(), 90));
/* 321 */       currData.add(info); b++; }
/*     */     
/* 323 */     paging.setCurrData(currData);
/*     */   }
/*     */   
/*     */   public String search(Map<String, String> parameterMap) {
/* 327 */     BooleanQuery bqs = new BooleanQuery();
/*     */     
/* 329 */     String stime = parameterMap.get("stime");
/* 330 */     String etime = parameterMap.get("etime");
/* 331 */     if (StringUtils.isNotEmpty(stime) || StringUtils.isNotEmpty(etime)) {
/*     */       
/* 333 */       if (StringUtils.isEmpty(stime)) {
/*     */         
/* 335 */         stime = "19700101";
/*     */       }
/*     */       else {
/*     */         
/* 339 */         stime = stime.replaceAll("-", "");
/*     */       } 
/*     */       
/* 342 */       if (StringUtils.isEmpty(etime)) {
/*     */         
/* 344 */         etime = (new SimpleDateFormat("yyyyMMdd")).format(new Date());
/*     */       }
/*     */       else {
/*     */         
/* 348 */         etime = etime.replaceAll("-", "");
/*     */       } 
/* 350 */       TermRangeQuery rangeQuery = new TermRangeQuery("sdate", stime, etime, true, true);
/* 351 */       bqs.add((Query)rangeQuery, BooleanClause.Occur.MUST);
/* 352 */       parameterMap.remove("stime");
/* 353 */       parameterMap.remove("etime");
/*     */     } 
/* 355 */     for (Map.Entry<String, String> entrySet : parameterMap.entrySet()) {
/* 356 */       String key = entrySet.getKey();
/* 357 */       String value = entrySet.getValue();
/* 358 */       if (StringUtils.isEmpty(value))
/*     */         continue; 
/* 360 */       List<String> keywords = LuceneTool.getParticiple(value);
/* 361 */       BooleanQuery bq = new BooleanQuery();
/* 362 */       for (String keyword : keywords)
/*     */       {
/* 364 */         bq.add((Query)new TermQuery(new Term(key, keyword)), BooleanClause.Occur.MUST);
/*     */       }
/* 366 */       bqs.add((Query)bq, BooleanClause.Occur.MUST);
/*     */     } 
/* 368 */     return bqs.toString();
/*     */   }
/*     */ }


/* Location:              D:\library\tomcat\webapps\ROOT\WEB-INF\classes\!\com\xhgk\dao\impl\PeriodicalDaoImpl.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       1.1.3
 */